<?
if (preg_match("#" . basename(__FILE__) . "#", $_SERVER['PHP_SELF'])) {
    die('This page cannot be called directly.');
}

class ajax_hotelreview
{
    function playme()
    {

        $code = Url::getParam('code');
        //yêu cầu đặt tên $code=tên của method trong class
        if (method_exists($this, $code)) {
            $this->$code();
        } else {
            $this->home();
        }
    }

    function home()
    {
        global $display;
        die("Nothing to do...");
    }

    function del_review()
    {
        $id = Url::getInt('review_id', 0);
        if ($id) {
            if (Db_HotelReview::delete($id)) {
                echo 'delete';
            }
        }
    }

    function show_review()
    {
        $id = Url::getInt('review_id', 0);
        if ($id) {
            if (Db_HotelReview::publish($id)) {
                echo 'publish';
            }
        }
    }

    function hide_review()
    {
        $id = Url::getInt('review_id', 0);
        if ($id) {
            if (Db_HotelReview::unpublish($id)) {
                echo 'unpublish';
            }
        }
    }

    function enable_review()
    {
        $id = Url::getInt('review_id', 0);
        if ($id) {
            if (Db_HotelReview::enable($id)) {
                echo 'enable';
            }
        }
    }

    function disable_review()
    {
        $id = Url::getInt('review_id', 0);
        if ($id) {
            if (Db_HotelReview::disable($id)) {
                echo 'disable';
            }
        }
    }

    function cus_review()
    {
        $cus_type = Url::getInt('cus_type', 0);
        $hotel_id = Url::getInt('hotel_id', 0);
        $review = Db_HotelReview::showReviewByCus($cus_type, $hotel_id);
        $reviews = array();
        foreach ($review as $review) {
            $review["cus_type"] = Db_HotelReview::getCusById($review["cus_type"]);
            array_unshift($reviews, $review);
        }
        echo json_encode($reviews);
    }

    function all_review()
    {
        $hotel_id = Url::getInt('hotel_id', 0);
        $review = Db_HotelReview::showall($hotel_id, ' status = 1 ');
        $reviews = array();
        foreach ($review as $review) {
            $review["cus_type"] = Db_HotelReview::getCusById($review["cus_type"]);
            array_unshift($reviews, $review);
        }
        echo json_encode($reviews);
    }

    function add_review()
    {
        if (User::is_login()) {
            $review = array();
            $review['hotel_id'] = Url::get('hotel_id');
            $review['user_id'] = User::$current->data['id'];
            $review['user_name'] = User::$current->data['full_name'];
            $review['address'] = User::$current->data['address'];
            $review['created_time'] = TIME_NOW;
            $review['cus_type'] = Url::get('cus_type');
            $review['comment_title'] = Url::get('title');
            $review['comment'] = Url::get('content');
            $review['rating_1_point'] = Url::getInt('score1');
            $review['rating_2_point'] = Url::getInt('score2');
            $review['rating_3_point'] = Url::getInt('score3');
            $review['rating_4_point'] = Url::getInt('score4');
            $review['rating_5_point'] = Url::getInt('score5');
            $review['rating_6_point'] = Url::getInt('score6');
            $sum = $review['rating_1_point'] + $review['rating_2_point'] + $review['rating_3_point'] + $review['rating_4_point'] + $review['rating_5_point'] + $review['rating_6_point'];
            $review['rating_0_point'] = round($sum / 6, 1);
            $review['status'] = 2;
            $review['active'] = 0;
            $review['show_in_total'] = 1;

            $valid = true;
            if ($review['comment'] == '' || $review['comment_title'] == '') {
                $valid = false;
            }
            if ($valid) {
                if ($a = Db_HotelReview::insert($review)) {
//                    echo $a;
                    Db_HotelReview::disable($a);
                    echo 1;
                } else {
                    echo 0;
                }
            }
        } else {
            echo 2;
        }
    }

    function add_travel_review()
    {
        if (User::is_login()) {
            $review = array();
            $review['news_id'] = Url::get('hotel_id');
            $review['user_id'] = User::$current->data['id'];
            $review['user_name'] = User::$current->data['full_name'];
            $review['address'] = User::$current->data['address'];
            $review['created_time'] = TIME_NOW;
            $review['tittle'] = Url::get('title');
            $review['content'] = Url::get('content');
            $review['status'] = 2;

            $valid = true;
            if ($review['tittle'] == '' || $review['content'] == '') {
                $valid = false;
            }
            if ($valid) {
                if (Db_News::insertreview($review)) {
                    echo 1;
                } else {
                    echo 0;
                }
            }
        } else {
            echo 2;
        }
    }

    function all_travel_review()
    {
        $hotel_id = Url::getInt('hotel_id', 0);
        $review = Db_News::showallreview($hotel_id, ' status = 1 ');
        $reviews = array();
        foreach ($review as $review) {
            array_unshift($reviews, $review);
        }
        echo json_encode($reviews);
    }

}

?>